package com.google.zxing.client.android.came2;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

import com.google.zxing.Result;
import com.google.zxing.client.android.R;
import com.google.zxing.client.android.ViewfinderView;

public class ScanActivity extends Activity {

    final String TAG = "ScanActivity";
    ViewfinderView viewfinderView;
    SurfaceView mSurfaceView;
    SurfaceHolder.Callback callback;
    boolean hasSurface = false;
    SurfaceHolder holder;
    CameraDeviceManager manager;
    ScanView mScanView;
    CaptureCamra2Handler mHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_scan);
        //viewfinderView = findViewById(R.id.viewfinder_view);
        mScanView = findViewById(R.id.scanview);
        mSurfaceView = findViewById(R.id.preview_view);
        holder = mSurfaceView.getHolder();
        callback = new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                if (holder == null) {
                    Log.e(TAG, "*** WARNING *** surfaceCreated() gave us a null surface!");
                }
                if (!hasSurface){
                    hasSurface = true;
                    initCamera(holder);
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                hasSurface = false;
            }
        };
        manager = new CameraDeviceManager(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (hasSurface){
            initCamera(holder);
        }else {
            holder.addCallback(callback);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mHandler != null) {
            mHandler.quitSynchronously();
            mHandler = null;
        }

        manager.closeDriver();
        if (hasSurface){
            holder.removeCallback(callback);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initCamera(SurfaceHolder holder){
        if (holder == null) {
            throw new IllegalStateException("No SurfaceHolder provided");
        }
        if (manager.isopen()) {
            Log.w(TAG, "initCamera() while already open -- late SurfaceView callback?");
            return;
        }

        try{
            manager.openDriver(holder);

            mHandler = new CaptureCamra2Handler(this,null,
                    null,null,manager);
        }catch (Exception e){}
    }

    public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
        Toast.makeText(this,rawResult.getText(),Toast.LENGTH_LONG).show();
        finish();
    }
}
